<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/7
 * Time: 17:30
 */

namespace app\web\controller;

use \think\Controller;
use \app\common\model\DivinationTips;

/**
 * 小米应用市场活动链接
 * Class DivinationMi
 * @package app\web\controller
 */
class DivinationMi extends Controller
{

    //提交问题
    public function get()
    {
        $data['question'] = htmlspecialchars(trim(input('question')));

        $validate = validate('DivinationMi');
        if ($validate->check($data) !== true)
        {
            \think\Log::write('提问错误:' . $validate->getError(), 'error_divination', true);
            return show(config('code.error'), '错误请求');
        }

        $_arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 16];// 行星id随机数种子

        $data['planet'] = $_arr[rand(0, count($_arr) - 1)];
        $data['sign'] = rand(1, 12);
        $data['house'] = rand(1, 12);
        $data['deg'] = round(randomFloat(0, 29), 8);
        $data['time'] = time();

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * @title 骰子经典模式
     * @method GET
     * @return int time 提问时间 时间戳/秒
     * @return int planet 行星id
     * @return int house 行星id
     * @return int sign 行星id
     * @return string love_status 骰子爱情状态
     * @return string marital_status 骰子婚姻状态 暂时去掉
     * @return string health_status 骰子健康状态
     * @return string trip_status 骰子出行状态
     * @return string money_status 骰子财富状态
     * @return string career_status 骰子事业状态
     * @return string study_status 骰子学业状态
     * @return string planet_text 骰子行星词条
     * @return string house_text 骰子宫位词条
     * @return string sign_text 骰子星座词条
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @readme /doc/md/divination/dice.md
     */
    public function dice()
    {
        $dice['planet'] = input('get.planet/d');
        $dice['sign'] = input('get.sign/d');
        $dice['house'] = input('get.house/d');
        $dice['deg'] = input('get.planet/f');
        $dice['time'] = input('get.time/d');

        try
        {
            //计算骰子六种模式
            $dice_status = $this->diceStatus($dice['planet'], $dice['sign'], $dice['house'], $dice['deg']);

            $result = [
                'time' => $dice['time'],
                'planet' => $dice['planet'],
                'house' => $dice['house'],
                'sign' => $dice['sign'],
                'love_status' => $dice_status['love'],
                'health_status' => $dice_status['health'],
                'trip_status' => $dice_status['trip'],
                'money_status' => $dice_status['money'],
                'career_status' => $dice_status['career'],
                'study_status' => $dice_status['study'],
            ];

            //根据提问详情获取骰子词条
            $DivinationTips = new DivinationTips();
            $result['planet_text'] = $DivinationTips->getDetail('p-' . $dice['planet']);
            $result['sign_text'] = $DivinationTips->getDetail('s-' . $dice['sign']);
            $result['house_text'] = $DivinationTips->getDetail('h-' . $dice['house']);
//            $result['url'] = 'https://weidian.com/item.html?itemID=2552906806';// 微店链接
//            $result['url'] = 'https://weidian.com/item.html?itemID=4475663455';// 微店链接（由于 微店问题，先改为这个地址，未来 改为 原地址【因为老的地址有评价】）
//            $result['url'] = 'https://weidian.com/item.html?itemID=4479316612';// 微店链接（由于原来的骰子，微店不能有迷信的产品，所以改为了这个）
//            $result['url'] = 'https://oss.goddessxzns.com/app/dice_ask.jpg';// 微店被封了，先替换为联系微信的，【改为草料活码，不好使，转到oss里refer失败】
//            $result['url'] = 'https://qr34.cn/EGF7t4';// 上面的操作，安卓的webview不能直接加载图片
            $result['url'] = 'https://wechat.goddessxzns.com/web/dice_ask/index.html';// 安卓跳两下，写死

            return show(config('code.success'), '加载成功', $result);

        }
        catch (\Exception $e)
        {
            \think\Log::write('骰子经典模式查询失败' . $e->getMessage(), 'divination_error', true);
            return show(config('code.error'), '加载失败');
        }

    }

    /**
     * 骰子吉凶
     * @param int $planet 行星id
     * @param int $sign 星座id
     * @param int $house 宫位id
     * @param float $deg 落星座度数
     * @return array
     */
    private function diceStatus($planet = null, $sign = null, $house = null, $deg = null)
    {
        // 初始化
        $score = [
            'love' => 0,
            'marital' => 0,
            'health' => 0,
            'trip' => 0,
            'money' => 0,
            'career' => 0,
            'study' => 0,
        ];
        // 计算行星得分
        switch ($planet)
        {
            case 0:
                $score['love'] += 4;
                $score['marital'] += 9;
                $score['health'] += 10;
                $score['trip'] += 10;
                $score['money'] += 2;
                $score['career'] += 10;
                $score['study'] += 10;
                break;
            case 1:
                $score['love'] += 5;
                $score['marital'] += 10;
                $score['health'] += -2;
                $score['trip'] += 1;
                $score['money'] += 8;
                $score['career'] += 3;
                $score['study'] += 1;
                break;
            case 2:
                $score['love'] += 2;
                $score['marital'] += 2;
                $score['health'] += 2;
                $score['trip'] += 9;
                $score['money'] += 1;
                $score['career'] += 2;
                $score['study'] += 9;
                break;
            case 3:
                $score['love'] += 10;
                $score['marital'] += 8;
                $score['health'] += 9;
                $score['trip'] += 9;
                $score['money'] += 10;
                $score['career'] += 4;
                $score['study'] += 9;
                break;
            case 4:
                $score['love'] += -5;
                $score['marital'] += -3;
                $score['health'] += -6;
                $score['trip'] += -8;
                $score['money'] += -5;
                $score['career'] += -3;
                $score['study'] += -8;
                break;
            case 5:
                $score['love'] += 8;
                $score['marital'] += 4;
                $score['health'] += 8;
                $score['trip'] += 10;
                $score['money'] += 9;
                $score['career'] += 10;
                $score['study'] += 10;
                break;
            case 6:
                $score['love'] += -10;
                $score['marital'] += 1;
                $score['health'] += -7;
                $score['trip'] += -3;
                $score['money'] += -7;
                $score['career'] += -4;
                $score['study'] += -3;
                break;
            case 7:
                $score['love'] += -10;
                $score['marital'] += -10;
                $score['health'] += -2;
                $score['trip'] += 5;
                $score['money'] += 5;
                $score['career'] += 1;
                $score['study'] += 5;
                break;
            case 8:
                $score['love'] += -8;
                $score['marital'] += -10;
                $score['health'] += -4;
                $score['trip'] += -2;
                $score['money'] += -6;
                $score['career'] += -10;
                $score['study'] += -2;
                break;
            case 9:
                $score['love'] += -6;
                $score['marital'] += -6;
                $score['health'] += -10;
                $score['trip'] += -4;
                $score['money'] += 6;
                $score['career'] += -7;
                $score['study'] += -4;
                break;
            case 12:
                $score['love'] += 9;
                $score['marital'] += 9;
                $score['health'] += 9;
                $score['trip'] += 9;
                $score['money'] += 9;
                $score['career'] += 9;
                $score['study'] += 9;
                break;
            case 16:
                $score['love'] -= 9;
                $score['marital'] -= 9;
                $score['health'] -= 9;
                $score['trip'] -= 9;
                $score['money'] -= 9;
                $score['career'] -= 9;
                $score['study'] -= 9;
                break;
        }
        // 庙+5，旺+4，三分+2，界+1，面+0.5，浮游时吉星【金木】+1.5，凶星【火土】-1.5，陷落-4
        $sign_score = 0;
        $is_change = false;
        if ($planet <= 9)
        {
            $ancient = config('astrolog.ancient')[$sign];// 古典数据

            if ($ancient[0] == $planet)
            {
                $sign_score += 5;// 入庙 +5
                $is_change = true;
            }
            if ($ancient[1] == $planet)
            {
                $sign_score += 4;// 擢升 旺 +4
                $is_change = true;
            }
            if (in_array($planet, $ancient[2]))
            {
                $sign_score += 2;// 三分 +2
                $is_change = true;
            }
            for ($i = 0; $i < 6; $i++)
            {
                if (
                    $planet &&
                    ($deg >= $ancient[3]['default'][1][$i]
                        && $deg < $ancient[3]['default'][1][$i + 1])
                )
                {
                    $sign_score += 2;// 界 +1
                    $is_change = true;
                }
            }
            if ($ancient[4][floor($deg / 10)] == $planet)
            {
                $sign_score += 0.5;// 面 +0.5
                $is_change = true;
            }
            $fall = ($sign <= 6) ? ($sign + 6) : ($sign - 6);
            $fall_ancient = config('astrolog.ancient')[$fall];
            if ($fall_ancient[0] == $planet)
            {
                $sign_score -= 5;// 失势 -4
                $is_change = true;
            }
            if ($fall_ancient[1] == $planet)
            {
                $sign_score -= 4;// 落陷 -4
                $is_change = true;
            }
            // 天王星  入庙 失势
            if ($planet == 7)
            {
                if ($sign == 11)
                {
                    $sign_score += 5;
                    $is_change = true;
                } elseif ($sign == 5)
                {
                    $sign_score -= 5;
                    $is_change = true;
                }
            }
            // 海王星  入庙 失势
            if ($planet == 8)
            {
                if ($sign == 12)
                {
                    $sign_score += 5;
                    $is_change = true;
                } elseif ($sign == 6)
                {
                    $sign_score -= 5;
                    $is_change = true;
                }
            }
            // 冥王星  入庙 失势
            if ($planet == 9)
            {
                if ($sign == 8)
                {
                    $sign_score += 5;
                    $is_change = true;
                } elseif ($sign == 2)
                {
                    $sign_score -= 5;
                    $is_change = true;
                }
            }
        }
        // 游走状态
        if ($is_change === false)
        {
            if ($planet == 3 || $planet == 5) $sign_score += 1.5;
            elseif ($planet == 4 || $planet == 6) $sign_score -= 1.5;
        }

        $score['love'] += $sign_score;
        $score['marital'] += $sign_score;
        $score['health'] += $sign_score;
        $score['trip'] += $sign_score;
        $score['money'] += $sign_score;
        $score['career'] += $sign_score;
        $score['study'] += $sign_score;

        switch ($house)
        {
            case 1:
                $score['love'] *= 1.5;
                $score['marital'] *= 1.5;
                $score['health'] *= 2;
                $score['trip'] *= 1.5;
                $score['money'] *= 1.5;
                $score['career'] *= 1.5;
                $score['study'] *= 1.5;
                break;
            case 2:
                $score['love'] *= 1;
                $score['marital'] *= 1;
                $score['health'] *= 1;
                $score['trip'] *= 1;
                $score['money'] *= 2;
                $score['career'] *= 1;
                $score['study'] *= 1;
                break;
            case 3:
                $score['love'] *= 1;
                $score['marital'] *= 1;
                $score['health'] *= 1;
                $score['trip'] *= 2;
                $score['money'] *= 1;
                $score['career'] *= 1;
                $score['study'] *= 2;
                break;
            case 4:
                $score['love'] *= 1.1;
                $score['marital'] *= 1.2;
                $score['health'] *= 1;
                $score['trip'] *= 1;
                $score['money'] *= 1.2;
                $score['career'] *= 1;
                $score['study'] *= 1;
                break;
            case 5:
                $score['love'] *= 2;
                $score['marital'] *= 1.1;
                $score['health'] += 5;
                $score['trip'] *= 1;
                $score['money'] *= 1.5;
                $score['career'] *= 1;
                $score['study'] *= 1;
                break;
            case 6:
                $score['love'] -= 15;
                $score['marital'] -= 15;
                $score['health'] *= 2;
                $score['trip'] -= 12;
                $score['money'] -= 10;
                $score['career'] *= 1.3;
                $score['study'] *= 1.3;
                break;
            case 7:
                $score['love'] *= 2;
                $score['marital'] *= 2;
                $score['health'] *= 1;
                $score['trip'] *= 1.5;
                $score['money'] *= 1;
                $score['career'] *= 1.2;
                $score['study'] *= 1;
                break;
            case 8:
                $score['love'] -= 15;
                $score['marital'] -= 15;
                $score['health'] -= 20;
                $score['trip'] -= 15;
                $score['money'] *= 1.5;
                $score['career'] -= 10;
                $score['study'] -= 10;
                break;
            case 9:
                $score['love'] *= 1;
                $score['marital'] *= 1;
                $score['health'] += 5;
                $score['trip'] *= 2;
                $score['money'] *= 1;
                $score['career'] *= 1.1;
                $score['study'] *= 2;
                break;
            case 10:
                $score['love'] *= 1;
                $score['marital'] *= 1;
                $score['health'] += 5;
                $score['trip'] *= 1;
                $score['money'] *= 1.2;
                $score['career'] *= 2;
                $score['study'] += 5;
                break;
            case 11:
                $score['love'] *= 1.1;
                $score['marital'] *= 1;
                $score['health'] *= 1;
                $score['trip'] *= 1.2;
                $score['money'] *= 1.3;
                $score['career'] *= 1.5;
                $score['study'] += 5;
                break;
            case 12:
                $score['love'] -= 20;
                $score['marital'] -= 20;
                $score['health'] -= 20;
                $score['trip'] -= 20;
                $score['money'] -= 20;
                $score['career'] -= 20;
                $score['study'] -= 20;
                break;
        }

        $result = [];
        foreach ($score as $k => $s)
        {
            if ($s < -25) $result[$k] = '不顺';// 低于-25	凶 【凶=不顺】
            elseif ($s >= -25 && $s < -15) $result[$k] = '平平';// 低于-15	不利 【不利=平平】
            elseif ($s >= -15 && $s < -5) $result[$k] = '平顺';// 低于-5	不顺 【不顺=平顺】
            elseif ($s >= -5 && $s < 5) $result[$k] = '小吉';// 低于5	平 【平=小吉】
            elseif ($s >= 5 && $s < 15) $result[$k] = '吉';// 高于5	小吉 【小吉=吉】
            elseif ($s >= 15 && $s < 25) $result[$k] = '大利';// 高于15	吉 【吉=大利】
            elseif ($s >= 25) $result[$k] = '大吉';// 高于25	大吉
        }
        return $result;
    }
}
